﻿using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;

using dotNails.CodeGen.Models;

namespace dotNails.Util {

    sealed class SolutionUtil {
		public static void AddTemplatedFile(DTE2 application, ITemplatedFile templatedFile) {
			Project project = GetProjectByName(application.Solution.Projects, templatedFile.ProjectName);

			ProjectUtil.AddFromFile(project, templatedFile.RelativeDestinationFolder, templatedFile.FullDestinationFilePath);
		}

		//public static Solution CreateSolution(DTE dte, string name)
		//{
		//    Solution solution = dte.Solution;
		//    string solnName = String.Format("{0}.sln", name);
		//    string tempPath = System.IO.Path.GetTempPath();
		//    soln.Create(tempPath, solnName);

		//    soln.SaveAs(tempPath + solnName);

		//    return soln;
		//}

		public static Project GetWebProject(DTE2 application) {
			Project project = null;

			// Find the web project in the solution
			foreach (Project currentProject in application.Solution.Projects) {
				if (AddInUtil.IsWebProject(currentProject)) {
					project = currentProject;
					break;
				}
			}

			return project;
		}

		// TODO: Test this because it can't be right yet
		//private string GetProjectName(DTE2 application, FileTypeEnum fileType, string baseNamespace) {
		//    string projectName = baseNamespace;

		//    // If a BaseNamespace is provided, the convention is that the following projects exist in the Solution and the following folders used:
		//    //	* BaseNamespace.Models\*
		//    //	* BaseNamespace.Controllers\*
		//    //	* BaseNamespace.Web\Views\Controllername\*
		//    // If no BaseNamespace is provided, everything is placed within the web project, just like the base MVC templated solution.
			
		//    if (!string.IsNullOrEmpty(projectName)) {
		//        projectName += ".";
		//    }

		//    switch (fileType) {
		//        case FileTypeEnum.Model:
		//            projectName += "Models";
		//            break;
		//        case FileTypeEnum.Controller:
		//            projectName += "Controllers";
		//            break;
		//        case FileTypeEnum.View:
		//            projectName += "Views";
		//            break;
		//        default:
		//            break;
		//    }

		//    return projectName;
		//}

		public static Project GetProjectByName(Projects projects, string projectName) {
			Project project = null;

			foreach (Project currentProject in projects) {
				if (currentProject.Name == projectName) {
					project = currentProject;
					break;
				}
			}

			return project;
		}
    }
}
